using System;
using System.Web;
using System.Net.Mail;

/// <summary>
/// Summary description for MailManager
/// </summary>
/// 
namespace CE.Mail
{
    public class MyMailManager
    {
        #region Private Variable
        private MailMessage mail = null;
        private MailAddress mailAddress = null;
        private SmtpClient mailClient = null;
        private int clientPort = -1;
        private string clientHost = String.Empty;
        private bool isEnabledSsl = false;
        private int mailType = -1;
        private string mailBody = "";
        private MailPriority mailPriority = MailPriority.Normal;

        static string smtphost = CE.Configuration.MyConfigurationManager.GetAppSettingValue("SMTPHostName");
        static string smtpport = CE.Configuration.MyConfigurationManager.GetAppSettingValue("SMTPPort");
        static string smtpaccount = CE.Configuration.MyConfigurationManager.GetAppSettingValue("SMTPAccount");
        static string smtpauthcode = CE.Configuration.MyConfigurationManager.GetAppSettingValue("SMTPAuthCode");
        static bool smtpenablessl = Convert.ToBoolean(CE.Configuration.MyConfigurationManager.GetAppSettingValue("SMTPSslEnable"));

        #endregion

        public MyMailManager()
        {
            mailClient = new SmtpClient();
            mailClient.Host = "smtp.gmail.com";
            mailClient.Port = 465;
            mailClient.EnableSsl = true;
        }

        public MyMailManager(string HostName, string Port, bool EnableSSL, string Account, string AuthCode)
        {
            smtphost = HostName;
            smtpenablessl = EnableSSL;
            smtpport = Port;
            smtpaccount = Account;
            smtpauthcode = AuthCode;
        }

        public static bool SendEmailByAttachedment(string PName, string ToEmailAddress, string CCEmailAddress, string BCCEmailAddress, string InsType, string Date, string Time, string RefNo, string[] fileList)
        {
            Uri myUri = new Uri(HttpContext.Current.Request.Url.ToString());

            string domainUrl = "http://" + myUri.Host;
            string ConfirmPath = CE.Configuration.MyConfigurationManager.GetAppSettingValue("ProviderPath");

            string confirmUrl = domainUrl;
            string imgUrl = domainUrl;
            if (myUri.Port != 80 || myUri.Port != 0)
            {
                confirmUrl += ":" + myUri.Port + ConfirmPath;
                imgUrl += ":" + myUri.Port;
            }
            else
            {
                confirmUrl += ConfirmPath;
            }

            imgUrl += CE.Configuration.MyConfigurationManager.GetAppSettingValue("LogoPath");


            string mailBody = "<br>Dear " + PName + ",<br><br>";
            mailBody += "<br>You have just received a completed " + InsType + " (Reference No : " + RefNo + ")";
            mailBody += " online form via <a href='http://www.CompareXpress.com' target='_blank'>CompareXpress.com</a>, dated ";
            mailBody += Date + " timed " + Time + ".";
            mailBody += "<br><br><b>Check Car Insurance related documents of Etiqa Insurance as attachments.</b>";
            mailBody += "<br><br><br>If you have further queries, please contact us at:";
            mailBody += "<br><br>CompareXpress Pte Ltd";
            mailBody += "<br>Blk 750B Chai Chee Road";
            mailBody += "<br>#04-05 to 08 Technopark@Chai Chee";
            mailBody += "<br>Singapore 469002";
            mailBody += "<br>Tel: 6309 1688";
            mailBody += "<br>Email: support@comparexpress.com";
            mailBody += "<br><br><br>Best regards,";
            mailBody += "<br><br><img src='" + imgUrl + "' />";
            mailBody += "<br><b>CompareXpress Team</b>";


            bool _result = SendMailNewByAttachments(smtphost,
                        465,
                        smtpaccount,
                        smtpauthcode,
                        "<CX> Attache documents for Car Insurance realted documents",
                        smtpaccount,
                        PName,
                        ToEmailAddress,
                        CCEmailAddress, 
                        BCCEmailAddress,
                        InsType + "Car Insurance Related Documents - from CompareXpress.Com",
                        mailBody,
                        smtpenablessl, fileList);
            return _result;
            //return true;
        }

        public bool SendEmailByAttachedmentNew(string customername, string email, string emailcc, string emailbcc, string subject, string emailBody, string[] fileList)
        {
            //Uri myUri = new Uri(HttpContext.Current.Request.Url.ToString());

            //string domainUrl = "http://" + myUri.Host;
            //string ConfirmPath = CE.Configuration.MyConfigurationManager.GetAppSettingValue("ProviderPath");

            //string confirmUrl = domainUrl;
            //string imgUrl = domainUrl;
            //if (myUri.Port != 80 || myUri.Port != 0)
            //{
            //    confirmUrl += ":" + myUri.Port + ConfirmPath;
            //    imgUrl += ":" + myUri.Port;
            //}
            //else
            //{
            //    confirmUrl += ConfirmPath;
            //}

            //imgUrl += CE.Configuration.MyConfigurationManager.GetAppSettingValue("LogoPath");


            bool _result = SendMailNewByAttachments(smtphost,
                        Convert.ToInt32(smtpport),
                        smtpaccount,
                        smtpauthcode,
                        subject,
                        smtpaccount,
                        customername,
                        email,
                        emailcc,
                        emailbcc,
                        subject,
                        emailBody,
                        smtpenablessl, fileList);
            return _result;
            //return true;
        }

        public static bool SendConfirmation(string UserName, string EmailAddress, string rowGUID)
        {
            Uri myUri = new Uri(HttpContext.Current.Request.Url.ToString());

            string domainUrl = "http://" + myUri.Host;
            string ConfirmPath = CE.Configuration.MyConfigurationManager.GetAppSettingValue("ConfirmCustomerPath");

            string confirmUrl = domainUrl;
            string imgUrl = domainUrl;
            if (myUri.Port != 80 || myUri.Port != 0)
            {
                confirmUrl += ":" + myUri.Port + ConfirmPath;
                imgUrl += ":" + myUri.Port;
            }
            else
            {
                confirmUrl += ConfirmPath;
            }

            imgUrl += CE.Configuration.MyConfigurationManager.GetAppSettingValue("LogoPath");

            string mailBody = "<img src='" + imgUrl + "' /><br>Your Account is : <b>" + UserName + "</b><br>";
            //mailBody += "Your Password is : <b>" + txtPassword.Text + "</b><br><br>";
            mailBody += "<br><a href='" + confirmUrl + "?confirmKey=" + rowGUID + "'>Click Here</a> to activate your Customer Account.";
            mailBody += "<br><br><br>Thank you very much!";
            mailBody += "<br>Regards,";
            mailBody += "<br>CompareXpress Membership Service";

            bool _result = SendMailNew(smtphost,
                        465,
                        smtpaccount,
                        smtpauthcode,
                        "CompareXpress Membership Service",
                        smtpaccount,
                        "Customer",
                        EmailAddress,
                        "",
                        "",
                        "Customer Account Confirmation from CompareXpress.Com",
                        mailBody,
                        smtpenablessl);
            return _result;
        }

        public static bool SendProposalCompleteNotification(string PName, string EmailAddress, string CCEmail, string BCCEmail, string InsType, string Date, string Time, string RefNo)
        {
            Uri myUri = new Uri(HttpContext.Current.Request.Url.ToString());

            string domainUrl = "http://" + myUri.Host;
            string ConfirmPath = CE.Configuration.MyConfigurationManager.GetAppSettingValue("ProviderPath");

            string confirmUrl = domainUrl;
            string imgUrl = domainUrl;
            if (myUri.Port != 80 || myUri.Port != 0)
            {
                confirmUrl += ":" + myUri.Port + ConfirmPath;
                imgUrl += ":" + myUri.Port;
            }
            else
            {
                confirmUrl += ConfirmPath;
            }

            imgUrl += CE.Configuration.MyConfigurationManager.GetAppSettingValue("LogoPath");


            string mailBody = "<br>Dear " + PName + ",<br><br>";
            mailBody += "<br>You have just received a completed " + InsType + " (Reference No : " + RefNo + ")";
            mailBody += " online form via <a href='http://www.CompareXpress.com' target='_blank'>CompareXpress.com</a>, dated ";
            mailBody += Date + " timed " + Time + ".";
            mailBody += "<br><br><b>Download Application Form Instruction</b>";
            mailBody += "<br><br>1) Please click on the following link to download the proposal form: ";
            mailBody += "<a href='" + confirmUrl + "'>Click Here</a>.";
            mailBody += "<br>2) Enter your user ID and password to securely download the completed form details.";
            mailBody += "<br><br><br>If you have further queries, please contact us at:";
            mailBody += "<br><br>CompareXpress Pte Ltd";
            mailBody += "<br>Blk 750B Chai Chee Road";
            mailBody += "<br>#04-05 to 08 Technopark@Chai Chee";
            mailBody += "<br>Singapore 469002";
            mailBody += "<br>Tel: 6309 1688";
            mailBody += "<br>Email: support@comparexpress.com";
            mailBody += "<br><br><br>Best regards,";
            mailBody += "<br><br><img src='" + imgUrl + "' />";
            mailBody += "<br><b>CompareXpress Team</b>";


            bool _result = SendMailNew(smtphost,
                        465,
                        smtpaccount,
                        smtpauthcode,
                        "<CX> Email Confirmation to Participating Merchant",
                        smtpaccount,
                        PName,
                        EmailAddress,
                        CCEmail,
                        BCCEmail,
                        InsType + " Proposal form - from CompareXpress.Com",
                        mailBody,
                        smtpenablessl);
            return _result;
        }

        public static bool SendCarProposalAttachment(string PName, string ToEmailAddress, string CCEMailAddress, string BCCEMailAddress, string InsType, string Date, string Time, string RefNo, string ProviderCompany, string[] fileList)
        {
            Uri myUri = new Uri(HttpContext.Current.Request.Url.ToString());

            string domainUrl = "http://" + myUri.Host;
            string ConfirmPath = CE.Configuration.MyConfigurationManager.GetAppSettingValue("ProviderPath");

            string confirmUrl = domainUrl;
            string imgUrl = domainUrl;
            if (myUri.Port != 80 || myUri.Port != 0)
            {
                confirmUrl += ":" + myUri.Port + ConfirmPath;
                imgUrl += ":" + myUri.Port;
            }
            else
            {
                confirmUrl += ConfirmPath;
            }

            imgUrl += CE.Configuration.MyConfigurationManager.GetAppSettingValue("LogoPath");


            string mailBody = "<br>Dear " + PName + ",<br><br>";
            mailBody += "<br>You have just received a completed " + InsType + " (Reference No : " + RefNo + ")";
            mailBody += " online form via <a href='http://www.CompareXpress.com' target='_blank'>CompareXpress.com</a>, dated ";
            mailBody += Date + " timed " + Time + ".";
            mailBody += "<br><br><b>Check Car Insurance related documents of "+ProviderCompany+" Insurance as attachments.</b>";
            mailBody += "<br><br><br>If you have further queries, please contact us at:";
            mailBody += "<br><br>CompareXpress Pte Ltd";
            mailBody += "<br>Blk 750B Chai Chee Road";
            mailBody += "<br>#04-05 to 08 Technopark@Chai Chee";
            mailBody += "<br>Singapore 469002";
            mailBody += "<br>Tel: 6309 1688";
            mailBody += "<br>Email: support@comparexpress.com";
            mailBody += "<br><br><br>Best regards,";
            mailBody += "<br><br><img src='" + imgUrl + "' />";
            mailBody += "<br><b>CompareXpress Team</b>";


            bool _result = SendMailNewByAttachments(smtphost,
                        465,
                        smtpaccount,
                        smtpauthcode,
                        "<CX> Attach documents for Car Insurance realted documents",
                        smtpaccount,
                        PName,
                        ToEmailAddress,
                        CCEMailAddress,
                        BCCEMailAddress,
                        InsType + "Car Insurance Related Documents - from CompareXpress.Com",
                        mailBody,
                        smtpenablessl, fileList);
            return _result;
            //return true;
        }

        public static bool SendConfirmationCode(string UserName, string EmailAddress, string rowGUID)
        {
            Uri myUri = new Uri(HttpContext.Current.Request.Url.ToString());

            string domainUrl = "http://" + myUri.Host;
            string ConfirmPath = CE.Configuration.MyConfigurationManager.GetAppSettingValue("ConfirmCustomerPath");

            string confirmUrl = domainUrl;
            string imgUrl = domainUrl;
            if (myUri.Port != 80 || myUri.Port != 0)
            {
                confirmUrl += ":" + myUri.Port + ConfirmPath;
                imgUrl += ":" + myUri.Port;
            }
            else
            {
                confirmUrl += ConfirmPath;
            }

            string[] arrGUIDCode = rowGUID.Split('-');
            string strGUIDCode = arrGUIDCode[0].ToString().ToUpper();

            imgUrl += CE.Configuration.MyConfigurationManager.GetAppSettingValue("LogoPath");

            string mailBody = "<img src='" + imgUrl + "' /><br>Your Account is : <b>" + UserName + "</b><br>";
            //mailBody += "Your Password is : <b>" + txtPassword.Text + "</b><br><br>";
            mailBody += "<br>Please use following confirmation code to answer when you login again.";
            mailBody += "<br><br/>Confirmation Code : <strong>" + strGUIDCode + "</strong>";
            mailBody += "<br><br><br>Thank you very much!";
            mailBody += "<br>Regards,";
            mailBody += "<br>CompareXpress Membership Service";

            bool _result = SendMailNew(smtphost,
                        465,
                        smtpaccount,
                        smtpauthcode,
                        "CompareXpress Membership Service",
                        smtpaccount,
                        "Customer",
                        EmailAddress,
                        "",
                        "",
                        "Customer Account Confirmation from CompareXpress.Com",
                        mailBody,
                        smtpenablessl);
            return _result;
        }

        public static bool SendPasswordResetLink(string UserName, string EmailAddress, string AuthSalt, string UniquiId, CE.Global.CEUserType UserType)
        {
            Uri myUri = new Uri(HttpContext.Current.Request.Url.ToString());

            string domainUrl = "http://" + myUri.Host;
            string ConfirmPath = "";
            if (UserType == CE.Global.CEUserType.Customer)
            {
                ConfirmPath = CE.Configuration.MyConfigurationManager.GetAppSettingValue("ResetCustomerPasswordPath");
            }
            else if (UserType == CE.Global.CEUserType.Provider)
            {
                ConfirmPath = CE.Configuration.MyConfigurationManager.GetAppSettingValue("ResetProviderPasswordPath");
            }

            string confirmUrl = domainUrl;
            string imgUrl = domainUrl;
            if (myUri.Port != 80 || myUri.Port != 0)
            {
                confirmUrl += ":" + myUri.Port + ConfirmPath;
                imgUrl += ":" + myUri.Port;
            }
            else
            {
                confirmUrl += ConfirmPath;
            }

            imgUrl += CE.Configuration.MyConfigurationManager.GetAppSettingValue("LogoPath");

            string mailBody = "<img src='" + imgUrl + "' /><br>Your Account is : <b>" + UserName + "</b><br>";
            //mailBody += "Your Password is : <b>" + txtPassword.Text + "</b><br><br>";
            mailBody += "<br><a href='" + confirmUrl + "?authkey=" + AuthSalt + "&uid=" + UniquiId + "'>Click Here</a> to reset your Customer Account's Password.";
            mailBody += "<br><br><br>Thank you very much!";
            mailBody += "<br>Regards,";
            mailBody += "<br>CompareXpress Membership Service";

            bool _result = SendMailNew(smtphost,
                        465,
                        smtpaccount,
                        smtpauthcode,
                        "CompareXpress Membership Service",
                        smtpaccount,
                        "Customer",
                        EmailAddress,
                        "",
                        "",
                        "Password Recovery - CompareXpress.Com",
                        mailBody,
                        smtpenablessl);
            return _result;
        }

        public static bool SendConfirmation(string EmailAddress, string EmailSubject, string EmailMessage, string rowGUID, string ConfirmPath)
        {
            Uri myUri = new Uri(HttpContext.Current.Request.Url.ToString());

            string domainUrl = "http://" + myUri.Host;

            string confirmUrl = domainUrl;
            string imgUrl = domainUrl;
            if (myUri.Port != 80 || myUri.Port != 0)
            {
                confirmUrl += ":" + myUri.Port + ConfirmPath;
                imgUrl += ":" + myUri.Port;
            }
            else
            {
                confirmUrl += ConfirmPath;
            }

            imgUrl += CE.Configuration.MyConfigurationManager.GetAppSettingValue("LogoPath");
            string mailBody = "<img src='" + imgUrl + "' /><br>";
            mailBody += EmailMessage;
            mailBody += "<br><a href='" + confirmUrl + "?confirmKey=" + rowGUID + "'><b>Click Here</b></a> to activate your account.";

            mailBody += "<br><br><br>Thank you very much!";
            mailBody += "<br>Regards,";
            mailBody += "<br><b>CompareXpress Membership Service</b>";
            mailBody += "<br><b>CompareXpress.com</b><br>";



            bool _result = SendMailNew(smtphost,
                        465,
                        smtpaccount,
                        smtpauthcode,
                        "CompareExpress",
                        smtpaccount,
                        "Member",
                        EmailAddress,
                        "",
                        "",
                        EmailSubject,
                        mailBody,
                        smtpenablessl);
            return _result;
        }


        public static bool SendFeedBack(string EmailAddress, string EmailSubject, string EmailMessage)
        {
            Uri myUri = new Uri(HttpContext.Current.Request.Url.ToString());

            string domainUrl = "http://" + myUri.Host;

            //string confirmUrl = domainUrl;
            string imgUrl = domainUrl;
            if (myUri.Port != 80 || myUri.Port != 0)
            {
                //confirmUrl += ":" + myUri.Port + ConfirmPath;
                imgUrl += ":" + myUri.Port;
            }
            else
            {
                //confirmUrl += ConfirmPath;
            }

            imgUrl += CE.Configuration.MyConfigurationManager.GetAppSettingValue("LogoPath");
            string mailBody = "<img src='" + imgUrl + "' /><br>";
            mailBody += EmailMessage;
            //mailBody += "<br><a href='" + confirmUrl + "?confirmKey=" + rowGUID + "'><b>Click Here</b></a> to activate your account.";

            mailBody += "<br><br><br>Thank you very much!";
            mailBody += "<br>Regards,";
            mailBody += "<br><b>Feedback Mail Service</b>";
            mailBody += "<br><b>CompareXpress.com</b><br>";



            bool _result = SendMailNew(smtphost,
                        465,
                        smtpaccount,
                        smtpauthcode,
                        "CompareXpress",
                        smtpaccount,
                        "Mail Service",
                        EmailAddress,
                        "",
                        "",
                        EmailSubject,
                        mailBody,
                        smtpenablessl);
            return _result;
        }

        public void CreateMail(string mailFrom, string mailTo, string mailName, MailPriority mailPriority)
        {
            mail = new MailMessage();
            mail.From = new MailAddress(mailFrom, mailName, System.Text.Encoding.UTF8);
            mail.To.Add(mailTo);
            //mail.To.Add("pkthant@gmail.com");
            mail.Priority = mailPriority;
            mailClient.Credentials = new System.Net.NetworkCredential(mailFrom, "brainstorming");
        }

        public void CreateMessage(string mailSubject, string mailBody)
        {
            mail.Subject = mailSubject;
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            mail.Body = mailBody;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;
        }

        public bool SendMail()
        {
            try
            {
                mailClient.Send(mail);
            }
            catch (Exception Ex)
            {
                return false;
            }

            return true;
        }

        public static bool SendMailNew(string sHost, int nPort, string sUserName, string sPassword, string sFromName, string sFromEmail,
        string sToName, string sToEmail, string sCCEmail, string sBCCEmail, string sHeader, string sMessage, bool fSSL)
        {
            if (sToName.Length == 0)
                sToName = sToEmail;
            if (sFromName.Length == 0)
                sFromName = sFromEmail;

            System.Web.Mail.MailMessage Mail = new System.Web.Mail.MailMessage();
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = sHost;
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = nPort.ToString();
            if (fSSL)
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";

            if (sUserName.Length == 0)
            {
                //Ingen auth 
            }
            else
            {
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = sUserName;
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = sPassword;
            }

            Mail.To = sToEmail;
            Mail.Cc = sCCEmail;
            Mail.Bcc = sBCCEmail;
            Mail.From = sFromEmail;
            Mail.Subject = sHeader;
            Mail.Body = sMessage;
            Mail.BodyFormat = System.Web.Mail.MailFormat.Html;
            try
            {
                System.Web.Mail.SmtpMail.SmtpServer = sHost;
                System.Web.Mail.SmtpMail.Send(Mail);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public static bool SendMailNewByAttachments(string sHost, int nPort, string sUserName, string sPassword, string sFromName, string sFromEmail,
        string sToName, string sToEmail, string sToCCEmail, string sToBCCEmail, string sHeader, string sMessage, bool fSSL, string[] fileList)
        {
            if (sToName.Length == 0)
                sToName = sToEmail;
            if (sFromName.Length == 0)
                sFromName = sFromEmail;

            System.Web.Mail.MailMessage Mail = new System.Web.Mail.MailMessage();
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = sHost;
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = nPort.ToString();
            if (fSSL)
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";

            if (sUserName.Length == 0)
            {
                //Ingen auth 
            }
            else
            {
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = sUserName;
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = sPassword;
            }

            Mail.To = sToEmail;
            Mail.From = sFromEmail;
            Mail.Subject = sHeader;
            Mail.Body = sMessage;
            Mail.Cc = sToCCEmail;
            Mail.Bcc = sToBCCEmail;

            for (int i = 0; i < fileList.Length; i++)
            {
                Mail.Attachments.Add(new System.Web.Mail.MailAttachment(fileList[i].ToString()));
            }

            Mail.BodyFormat = System.Web.Mail.MailFormat.Html;
            try
            {
                System.Web.Mail.SmtpMail.SmtpServer = sHost;
                System.Web.Mail.SmtpMail.Send(Mail);
            }
            catch (Exception Ex)
            {
                Ex.ToString();
                return false;
            }
            return true;
        }
    }
}
